package com.lingshu.app.ui.welcome;

import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.lifecycle.ViewModelProvider;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.lingshu.app.R;
import com.lingshu.app.base.BaseActivity;
import com.lingshu.app.databinding.ActivityWelcomeGuideBinding;
import com.lingshu.app.ui.login.LoginActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 于定金
 * @version 1.0
 * @Date 2024/1/3 11:19
 * @Description
 */
public class WelcomeGuideActivity extends BaseActivity<ActivityWelcomeGuideBinding, WelcomeGuideViewModel> {
    private WelcomeGuideActivityAdapter adapter;
    private ViewPager pager;
    private Button button;
    private List<View> list;

    /**
     * 获取当前页面的布局资源ID
     *
     * @return 布局资源ID
     */
    @Override
    protected int getLayoutResId() {
        return R.layout.activity_welcome_guide;
    }

    /**
     * 初始化ViewModel
     */
    @Override
    protected void initViewModel() {
        viewModel = new ViewModelProvider(this).get(WelcomeGuideViewModel.class);
    }

    /**
     * 绑定ViewModel
     */
    @Override
    protected void bindViewModel() {
        //dataBinding.setViewModel(viewModel);
    }

    @Override
    protected void init() {
        super.init();
        pager = findViewById(R.id.viewpager_guide_list);
        button = findViewById(R.id.btnEnter);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(WelcomeGuideActivity.this, LoginActivity.class));
            }
        });

        adapter = new WelcomeGuideActivityAdapter();
        list = new ArrayList<View>();
        ImageView v1 = new ImageView(this);
        v1.setImageResource(R.drawable.welcome_guide_01);
        v1.setScaleType(ImageView.ScaleType.CENTER);
        list.add(v1);
        ImageView v2 = new ImageView(this);
        v2.setImageResource(R.drawable.welcome_guide_02);
        v2.setScaleType(ImageView.ScaleType.CENTER);
        list.add(v2);

        ImageView v3 = new ImageView(this);
        v3.setImageResource(R.drawable.welcome_guide_02);
        v3.setScaleType(ImageView.ScaleType.CENTER);
        list.add(v3);
        pager.setAdapter(new WelcomeGuideActivityAdapter());
        pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 2) {
                    button.setVisibility(View.VISIBLE);
                } else {
                    button.setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    public class WelcomeGuideActivityAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public boolean isViewFromObject(@NonNull View arg0, @NonNull Object arg1) {
            return arg0 == arg1;
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            container.addView(list.get(position));
            return list.get(position);
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            //super.destroyItem(container, position, object);
            container.removeView(list.get(position));
        }
    }
}
